package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.GoodsInfoVO;
import com.chinairi.mall.controller.viewobject.GoodsPictureVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.FourNewGoods;
import com.chinairi.mall.model.GoodsPicture;
import com.chinairi.mall.service.IGoodsInfoService;
import com.chinairi.mall.service.IGoodsPictureService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 商品图片 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-05
 */
@RestController
@RequestMapping("/goods-picture")
public class GoodsPictureController extends BaseController {

    @Autowired
    private IGoodsPictureService iGoodsPictureService;

    @Autowired
    private IGoodsInfoService iGoodsInfoService;

    /**
     * 上传图片
     *
     * @param file           图片文件
     * @param goodsFileClass 图片类型：1主图，2副图
     * @param goodsDbid      商品id,(单独上传时)
     * @throws IOException
     */
    @PostMapping("/uploadPic")
    public void uploadPic(MultipartFile file, String goodsFileClass, String goodsDbid) throws IOException {
        GoodsPicture goodsPicture = iGoodsPictureService.uploadPic(file, goodsFileClass, goodsDbid);
        ResponseUtil.showMessage("success", "上传成功", goodsPicture);
    }

    /**
     * 储存图片信息
     *
     * @param goodsPicture 图片文件
     * @throws IOException
     */
    @PostMapping("/uploadPicNew")
    @ResponseBody
    public void uploadPicNew(@RequestBody GoodsPicture goodsPicture) throws IOException {
        iGoodsPictureService.uploadPicNew(goodsPicture);
        ResponseUtil.showMessage("success", "上传成功", null);
    }

    /**
     * 删除图片
     *
     * @param goodsFileDbid
     * @throws IOException
     */
    @DeleteMapping("/delete")
    public void delete(String goodsFileDbid) throws IOException {
        iGoodsPictureService.removeById(goodsFileDbid);
        ResponseUtil.showMessage("success", "删除完成", "");
    }

    /**
     * 店铺下获取商品名称
     *
     * @param
     */
    @GetMapping("/getGoodsName")
    public void getGoodsName(HttpServletRequest request) throws IOException {
        String csDbId = iGoodsInfoService.getCsDbid("0", request);
        List<GoodsInfoVO> goodsInfoVOList = iGoodsPictureService.getGoodsName(csDbId);
        ResponseUtil.showMessage("success", "获取完成", goodsInfoVOList);
    }

    /**
     * 店铺下获取商品名称
     *
     * @param
     */
    @GetMapping("/getFourNewName")
    public void getFourNewName(HttpServletRequest request) throws IOException {
        String csDbId = iGoodsInfoService.getCsDbid("0", request);
        List<FourNewGoods> goodsInfoVOList = iGoodsPictureService.getFourNewName(csDbId);
        ResponseUtil.showMessage("success", "获取完成", goodsInfoVOList);
    }

    /**
     * 查询图片
     *
     * @param goodsPictureVO
     * @throws IOException
     */
    @GetMapping("/searchPic")
    public void searchPic(GoodsPictureVO goodsPictureVO) throws IOException {
        List<GoodsPicture> goodsPictureList = iGoodsPictureService.searchPic(goodsPictureVO);
        ResponseUtil.showMessage("success", "获取成功", goodsPictureList);
    }


}
